<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="活动列表")
 */
class Activity extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Activity();
        
    }

     /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
              
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
 

    /**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('活动名称不能为空');
            }
            $data = [
                'title'=>$post['title'],
                'activity_time'=>$post['activity_time'],
                'desc'=>$post['desc'],
                'create_time'=>time()
            ];
            try {
                Db::name('activity')->insert($data); 
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            return $this->fetch();
        }
        
    }

     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['title'])){
                $this->error('活动名称不能为空');
            }
            $data = [
                'title'=>$post['title'],
                'activity_time'=>$post['activity_time'],
                'desc'=>$post['desc']
            ];
            try {
               $this->model->where('id',$post['id'])->update($data);
               
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功') ;
        }else{
            $post = $this->request->param(); 
            $row = $this->model->where('id',$post['id'])->find();
            $row['desc'] = html_entity_decode($row['desc']);
            $row['desc'] = str_replace(array("\r", "\n\n", "\s", "\t", "\n"), '<br/>', $row['desc']);
            
            $this->assign('row', $row);
        }
        return $this->fetch();
    }

}